﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_yahoo_List : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnDelete.Attributes.Add("onclick", "a=confirm('Đồng ý xóa?');document.getElementById('" + hidDelete.ClientID + "').value=a;");
        this.btnDuyet.Attributes.Add("onclick", "a=confirm('Đồng ý duyệt?');document.getElementById('" + this.Duyet.ClientID + "').value=a");
        
        if (!this.IsPostBack)
        {
            LoadDataGrid();
        }
    }

    private void LoadDataGrid()
    {
        
        WebData db = new WebData();
        this.grdDs.DataSource = db.tbl_Nick_Yahoos.GetAll();
        this.grdDs.DataBind();
    }


    private void Active() {
        try
        {
            int n = this.grdDs.Rows.Count;
            if (this.Duyet.Value == "true")
            {
                bool isselected = false;
                if (n > 0)
                {
                    CheckBox chk;
                    for (int i = 0; i < n; i++)
                    {
                        chk = (CheckBox)this.grdDs.Rows[i].FindControl("chkActive");
                        try
                        {
                                int _ID = Convert.ToInt32(this.grdDs.DataKeys[this.grdDs.Rows[i].RowIndex].Value);
                                WebData db = new WebData();
                                DataLibrary.tbl_Nick_Yahoo item = new DataLibrary.tbl_Nick_Yahoo(_ID);
                                item.IdNick = _ID;
                                item.IsActive = chk.Checked == true ? 1 : 0;
                                if(db.tbl_Nick_Yahoos.StatusOnSubmit(item)!=-1)
                                    isselected = true;
                        }
                        catch
                        {

                        }
                    }
                }
                if (isselected)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update + "');", true);
                    LoadDataGrid();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update_err + "');", true);
                }
            }
        }
        catch { }

    }

    private void Delete()
    {
       
        try {
            int n = this.grdDs.Rows.Count;
            if (this.hidDelete.Value == "true") {
                bool isselected = false;
                if (n > 0) {
                    CheckBox chk;
                    for (int i = 0; i < n; i++) {
                        chk = (CheckBox)this.grdDs.Rows[i].FindControl("chkDelete");
                        try {
                                if (chk.Checked)
                                {
                                    int _ID = Convert.ToInt32(this.grdDs.DataKeys[this.grdDs.Rows[i].RowIndex].Value);
                                    WebData db = new WebData();
                                    DataLibrary.tbl_Nick_Yahoo item = new DataLibrary.tbl_Nick_Yahoo(_ID);
                                   if(db.tbl_Nick_Yahoos.DeleteOnSubmit(item)!=-1)
                                        isselected = true;
                                }

                        }
                        catch { 

                        }
                    }
                }
                if (isselected) {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete + "');", true);
                    LoadDataGrid();
                }
                else {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete_err + "');", true);
                }
            }
        }
        catch { }
        
    }

    protected void grdDs_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    protected void btnDuyet_Click(object sender, ImageClickEventArgs e)
    {
        Active();
    }

    protected void btnimgDel_Click(object sender, ImageClickEventArgs e)
    {
        Delete();
        
    }
}